import os
import shutil
from cvRemap.utils import *

if __name__ == '__main__':
    rootpath = 'D:/00_indemind_lyk/ICE_I26R_I18R/depth/20210401_lyk_with_depth3/20210401_lyk_with_depth'
    yaml_name = "MoudleParam.yaml"
    yaml_path = os.path.join(rootpath, yaml_name)
    fisheye_init_x, fisheye_init_y = read_yaml(yaml_path)
    folder_paths = os.listdir(rootpath)
    for folder in folder_paths:
        if not "record" in folder:
            continue
        image_folder = os.path.join(rootpath, folder, "imsee_record.bag.imgs.L")
        out_folder = os.path.join(rootpath, folder, "remapL")
        if os.path.exists(out_folder):
            shutil.rmtree(out_folder)  # delete output folder
        os.makedirs(out_folder)  # make new output folder
        image_names = os.listdir(image_folder)

        for image_name in image_names:
            if 'png' not in image_name:
                continue
            image_path = os.path.join(image_folder, image_name)
            remap_image_path = os.path.join(out_folder, image_name)

            src_image = cv2.imread(image_path)
            remap_image = remap_eye(src_image, fisheye_init_x, fisheye_init_y)
            cv2.imwrite(remap_image_path, remap_image)
            # cv2.imshow("src", src_image)
            # cv2.imshow("remap_image", remap_image)
            # cv2.waitKey()

        # print(image_folder)
        print(remap_image_path)